package com.ruoyi.web.api;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.otc.domain.Business;
import com.ruoyi.otc.domain.Label;
import com.ruoyi.otc.service.IBusinessService;
import com.ruoyi.otc.service.ILabelService;
import com.ruoyi.web.api.base.ApiBaseController;
import com.ruoyi.web.api.vo.LabelVo;
import com.ruoyi.web.constant.ApiConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(value = "智能化标签管理接口",tags = "智能化标签管理接口")
@RestController
@RequestMapping(ApiConstant.API_V1+"label")
public class ApiLabelController extends ApiBaseController {

    @Autowired
    private ILabelService labelService;
    @Autowired
    private IBusinessService businessService;





    /**
     * 查询标签信息列表
     */
    @ApiOperation("查询标签信息列表")
    @PostMapping("/listAll")
    @ResponseBody
    public AjaxResult listAll()
    {
        JSONObject json = new JSONObject();
        int exist = businessService.count(new QueryWrapper<Business>().eq("exist", true));
        int longinCapital = businessService.count(new QueryWrapper<Business>().eq("longin_capital", true));
        int possess = businessService.count(new QueryWrapper<Business>().eq("possess", true));
        int rank = businessService.count(new QueryWrapper<Business>().eq("rank1", true));
        int wheth = businessService.count(new QueryWrapper<Business>().eq("wheth", true));
        int whether = businessService.count(new QueryWrapper<Business>().eq("whether", true));
        int qysf = businessService.count(new QueryWrapper<Business>().eq("qysf", true));
        int zjsf = businessService.count(new QueryWrapper<Business>().eq("zjsf", true));
        json.put("exist",exist);
        json.put("longinCapital",longinCapital);
        json.put("possess",possess);
        json.put("rank",rank);
        json.put("wheth",wheth);
        json.put("whether",whether);
        json.put("qysf",qysf);
        json.put("zjsf",zjsf);
        return AjaxResult.success(json);
    }








    /**
     * 查询标签信息列表
     */
    @ApiOperation("查询标签信息列表")
    @PostMapping("/List")
    @ResponseBody
    public TableDataInfo list(@RequestBody LabelVo label)
    {
        startPage();
        List<Label> list = labelService.list(new QueryWrapper<Label>()
                .like(StringUtils.isNotBlank(label.getName()),"name",label.getName()));
        List<Label> labels = new ArrayList<>();
        for (Label label1 : list) {
            int count = businessService.count(new QueryWrapper<Business>().eq(!"longinCapital".equals(label1.getName()),label1.getName(), true)
                                                                        .eq("longinCapital".equals(label1.getName()),"longin_capital",true));
//            label1.setCount(count);
            labels.add(label1);
        }
        labelService.saveOrUpdateBatch(labels);
        return getDataTable(list);
    }

    /**
     * 删除标签信息
     */
    @ApiOperation("删除标签信息")
    @PostMapping( "/remove/{id}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("id") Long id)
    {
        return toAjax(labelService.deleteLabelById(id));
    }
}
